#pragma once

class CBaseReader
{
public:
  CBaseReader() : m_nLine(0), m_bUtf8(false)
  {}

  virtual long GetPosition() const = 0;
  virtual bool SetPosition(long pos) = 0;

  virtual bool ReadLine(CString& s) = 0;
  virtual bool ReadPair(CString& key, CString& val, TCHAR delim = TEXT('='))
  {
    CString str;
    do
    {
      if (!ReadLine(str))
        return false;
    }
    while(str.IsEmpty() || str[0] == TEXT(';') || str.Find(TEXT("//")) == 0);

    int pos = str.Find(delim);
    if (pos >= 0)
    {
      key = str.Left(pos);
      val = str.Mid(pos + 1);
      key.Trim();
      val.Trim();
      return true;
    }
    return false;
  }
  virtual bool ReadPair(const TCHAR* key, CString& val, TCHAR delim = TEXT('='))
  {
    if (!key)
      return false;

    CString str;
    do
    {
      if (!ReadLine(str))
        return false;
    }
    while(str.IsEmpty() || str[0] == TEXT(';') || str.Find(TEXT("//")) == 0);

    int pos = str.Find(delim);
    if (pos >= 0)
    {
      CString rkey = str.Left(pos);
      if (rkey.CompareNoCase(key) != 0)
      {
        Logger::LogFmt(lsError, TEXT("Error reading file at line %d: '%s' found while expecting '%s'"), m_nLine, str.GetString(), key);
        return false;
      }
      val = str.Mid(pos + 1);
      val.Trim();
      return true;
    }
    return false;
  }

  virtual bool IsUtf8() const
  { return m_bUtf8; }
  virtual int GetLine() const
  { return m_nLine; }

protected:
  int   m_nLine;
  bool  m_bUtf8;
};